package longrise.phone.com.itssnew.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.LEAP.Base.Objects.EntityBeanSet;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import longrise.phone.com.itssnew.R;
import longrise.phone.com.itssnew.adapter.CompanyAdapter;
import longrise.phone.com.itssnew.bean.CompanyBean;
import longrise.phone.com.itssnew.bean.Constant;
import longrise.phone.com.itssnew.bean.JZSQGloble;
import longrise.phone.com.itssnew.utils.CommonUtil;
import longrise.phone.com.itssnew.utils.SPUtil;
import longrise.phone.com.itssnew.widget.LoadDialog;
import rx.functions.Action1;

import static longrise.phone.com.itssnew.bean.Constant.SPOPERATIONSORDESIGN;


public class CompanyActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnItemClickListener {
    @Bind(R.id.rv_company)
    RecyclerView rvCompany;
    @Bind(R.id.sf_company)
    SwipeRefreshLayout sfCompany;
    @Bind(R.id.top_right)
    TextView topRight;
    @Bind(R.id.company_button)
    Button companyButton;
    private CompanyAdapter mCompanyAdapter;
    private LoadDialog mLoadDialog;
    private boolean isSubmit;
    private List<CompanyBean> mCompanyBeens = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initSwipeRefreshLayoutAndRecyclerView();
        onRefresh();
        rxManager.on("refresh", new Action1<Object>() {
            @Override
            public void call(Object o) {
                CompanyActivity.this.onRefresh();
            }
        });
    }

    @Override
    public int bindLayoutId() {
        return R.layout.activity_company;
    }

    @Override
    public void onRefresh() {
        mCompanyBeens.clear();
        searchExperts();
    }

    private void initSwipeRefreshLayoutAndRecyclerView() {
        sfCompany.setOnRefreshListener(this);
        sfCompany.setColorSchemeColors(Color.rgb(47, 223, 189));
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvCompany.setLayoutManager(layoutManager);
        mCompanyAdapter = new CompanyAdapter(R.layout.activity_company_item, null);
        mCompanyAdapter.setOnItemClickListener(this);
        rvCompany.setAdapter(mCompanyAdapter);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        CompanyBean mCompanyBean = (CompanyBean) adapter.getItem(position);
        Intent mIntent;
        int runType = SPUtil.getSharedIntData(this, SPOPERATIONSORDESIGN);
        if (runType == 0) mIntent = new Intent(CompanyActivity.this, ScoreOperationsActivity.class);
        else mIntent = new Intent(CompanyActivity.this, ScoreDesignActivity.class);
        mIntent.putExtra("id", mCompanyBean.getId());
        mIntent.putExtra("code", mCompanyBean.getCode());
        mIntent.putExtra("isSubmit", isSubmit);
        startActivity(mIntent);
    }

    @OnClick({R.id.top_left, R.id.company_button})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.top_left:
                showLoginOutDialog(this);
                break;
            case R.id.company_button:
                showSubmitAllDateDialog();
                break;
        }
    }

    private void showSubmitAllDateDialog() {
        if (haveOneNotScore()) return;
        CommonUtil.showDialog(this, "确定提交?提交后所有打分将不可更改！", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submitAllData();
            }
        });
    }

    /**
     * 所有企业是否打分完成
     *
     * @return
     */
    private boolean haveOneNotScore() {
        boolean haveOneNotScore = false;
        for (CompanyBean mCompanyBean : mCompanyBeens) {
            if ("否".equals(mCompanyBean.getIsfillopinion())) {
                CommonUtil.showToast(CompanyActivity.this, "您有企业未打分完成");
                haveOneNotScore = true;
                break;
            }
        }
        return haveOneNotScore;

    }

    /**
     * 提交所有企业的打分数据
     */
    private void submitAllData() {
        if (loginInvalid(this)) return;
        mLoadDialog = new LoadDialog(this);
        mLoadDialog.show();
        JZSQGloble.getInstance(this).call("submitrate", Boolean.class, new JZSQGloble.OnCallFinishListener() {
            @Override
            public void onCallFinish(Object bean) {
                mLoadDialog.dismiss();
                if(null == bean) return;
                Boolean submitOver = (Boolean) bean;
                if (submitOver) CommonUtil.showToast(CompanyActivity.this, "提交成功");
                else CommonUtil.showToast(CompanyActivity.this, "提交失败");
                onRefresh();
            }
        }, SPUtil.getSharedIntData(this, SPOPERATIONSORDESIGN) + "");
    }


    /**
     * 查询专家信息
     */
    private void searchExperts() {
        if (loginInvalid(this)) return;
        mLoadDialog = new LoadDialog(this);
        mLoadDialog.show();
        JZSQGloble.getInstance(this).call("getExpName", String.class, new JZSQGloble.OnCallFinishListener() {
            @Override
            public void onCallFinish(Object bean) {
                if(bean == null)return;
                String experts = (String) bean;
                if (experts == null) experts = "";
                else experts = "专家:" + experts;
                topRight.setText(experts);
                searchCompany();
            }
        });
    }

    /**
     * 查询企业信息
     */
    private void searchCompany() {
        JZSQGloble.getInstance(this).call("getCompanyOfToday", EntityBeanSet.class, new JZSQGloble.OnCallFinishListener() {
            @Override
            public void onCallFinish(Object bean) {
                mLoadDialog.dismiss();
                if (sfCompany.isRefreshing()) {
                    sfCompany.setRefreshing(false);
                }
                if (bean == null) {
                    CommonUtil.showToast(CompanyActivity.this, "未查询到企业信息，请确认是否添加");
                    companyButton.setVisibility(View.GONE);
                    return;
                }
                EntityBeanSet mEntityBeans = (EntityBeanSet) bean;
                mCompanyBeens = getComPanyList(mEntityBeans);
                if (null == mCompanyBeens || mCompanyBeens.size() == 0)
                    CommonUtil.showToast(CompanyActivity.this, "未查询到数据");
                else {
                    isSubmit = mCompanyBeens.get(0).isSubmit();
                    if (isSubmit) companyButton.setVisibility(View.GONE);
                    else companyButton.setVisibility(View.VISIBLE);
                    mCompanyAdapter.setNewData(mCompanyBeens);
                }
            }
        }, SPUtil.getSharedIntData(this, Constant.SPOPERATIONSORDESIGN) + "");
    }

    /**
     * 转换为CompanyBean
     *
     * @param beans
     * @return
     */
    public ArrayList<CompanyBean> getComPanyList(EntityBeanSet beans) {
        ArrayList<CompanyBean> list = new ArrayList<CompanyBean>();
        for (int i = 0; i < beans.getSize(); i++) {
            EntityBean entityBean = beans.get(i);
            CompanyBean bean = new CompanyBean();
            bean.setName(entityBean.getString("name"));
            bean.setCode(entityBean.getString("shortname "));
            bean.setSum(entityBean.getString("sum"));
            bean.setId(entityBean.getString("id"));
            bean.setSubmit(entityBean.getBoolean("issubmit"));
            bean.setAllrateb(entityBean.getBoolean("allrateb"));
            bean.setIsfillopinion(entityBean.getString("isfillopinion"));
            bean.setNum(i);
            list.add(bean);
        }
        return list;
    }
}
